package com.ktgj.customer.controller;

import com.ktgj.common.core.utils.DateUtils;
import com.ktgj.common.core.web.controller.BaseController;
import com.ktgj.common.core.web.domain.AjaxResult;
import com.ktgj.common.log.annotation.SignatureValidation;
import com.ktgj.customer.common.AxInsuranceUtils;
import com.ktgj.customer.common.DxInsuranceUtils;
import com.ktgj.customer.domain.Customer;
import com.ktgj.customer.domain.Insurance.ApplyPolicyNoPayInput;
import com.ktgj.customer.domain.Insurance.axModel.*;
import com.ktgj.customer.domain.Insurance.axModel.RequestHeader;
import com.ktgj.customer.domain.Insurance.dxModel.DXInsured;
import com.ktgj.customer.domain.Insurance.dxModel.DXOrderInfo;
import com.ktgj.customer.domain.Insurance.dxModel.InsureRequest;
import com.ktgj.customer.domain.Insurance.dxModel.InsureResponse;
import com.ktgj.customer.domain.Insurance.hlModel.HLInsurance;
import com.ktgj.customer.domain.InsuranceRecord;
import com.ktgj.customer.domain.vo.InsuranceRecordVo;
import com.ktgj.customer.domain.vo.InsuranceReturnVo;
import com.ktgj.customer.domain.vo.ProductVo;
import com.ktgj.customer.service.IInsuranceRecordService;
import com.ktgj.customer.service.ILoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@ControllerAdvice
@RestController
@RequestMapping("/insuranceType")
public class InsuranceRecordController extends BaseController
{
    @Autowired
    private IInsuranceRecordService insuranceRecordService;

    @Autowired
    private ILoginService loginService;

    /**
     * 新增保险记录
     * */
    //@SignatureValidation
    @PostMapping("/insertInsuranceRecord")
    public AjaxResult insertInsuranceRecord(HttpServletRequest request, @RequestBody ProductVo productVo){
        if (productVo.customerId == null || productVo.customerId == 0){
            Customer customer;
            try {
                customer = loginService.getLoginCustomer(request);
            } catch (Exception e){
                return AjaxResult.expired("登录已过期，请重新登录");
            }

            productVo.customerId = customer.getCustomerId();
        }


        if (productVo.id != null && productVo.id != 0){
            InsuranceReturnVo insuranceReturnVo;
            try {
                insuranceReturnVo = insuranceRecordService.insertInsuranceRecord(productVo.customerId, productVo);
            } catch (Exception e){
                return AjaxResult.error(e.getMessage());
            }

            return AjaxResult.success(insuranceReturnVo);

        }
        else {
            return AjaxResult.error("未选择辅营产品");
        }
    }

    /**
     * 新增保险记录(远程)
     * */
    //@SignatureValidation
    @PostMapping("/insertInsuranceRecordBack")
    public InsuranceReturnVo insertInsuranceRecordBack(@RequestBody ProductVo productVo){

        if (productVo.id != null && productVo.id != 0){
            InsuranceReturnVo insuranceReturnVo;
            try {
                insuranceReturnVo = insuranceRecordService.insertInsuranceRecord(productVo.customerId, productVo);
            } catch (Exception e){
                e.printStackTrace();
                System.out.println(e.getMessage());
                return null;
            }

            return insuranceReturnVo;

        }
        else {
            System.out.println("未选择辅营产品");
            return null;
        }
    }

    /**
     * 后端——查询保险记录
     * */
    @PostMapping("/backInsuranceRecord")
    public AjaxResult backSelectInsuranceRecord(HttpServletRequest request){
        Long customerId;
        try {
            customerId = loginService.getLoginCustomer(request).getCustomerId();
        } catch (Exception e){
            return AjaxResult.expired("登录已过期，请重新登录");
        }
        List<InsuranceRecordVo> insuranceRecordVoList;
        try {
            insuranceRecordVoList = insuranceRecordService.backSelectInsuranceRecord(customerId);
        } catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }
        if (insuranceRecordVoList != null && insuranceRecordVoList.size() > 0){
            return AjaxResult.success(insuranceRecordVoList);
        }
        else {
            return AjaxResult.error("未查到数据");
        }
    }

    /**
     * 修改德付通保险状态（远程）
     * */
    @PostMapping("/updateStart")
    public boolean updateStart(@RequestBody InsuranceRecord insuranceRecord){
        int i;
        try {
            i = insuranceRecordService.updateStart(insuranceRecord);
        } catch (Exception e){
            return false;
        }
        if (i > 0){
            return true;
        }
        else {
            return false;
        }
    }
    //根据保险记录ID查询保险记录
    @PostMapping("/getInsuranceRecord")
    public AjaxResult getInsuranceRecord(@RequestBody Map<String, Long> map)
    {
        Long recordId = map.get("recordId");
        if (recordId != null)
        {
            InsuranceRecord record = insuranceRecordService.getInuseranceRecord(recordId);
            return AjaxResult.success(record);
        }
        return AjaxResult.error("查询保险记录失败！");
    }
}
